# -*- coding: utf-8 -*-
# filename: tcp_server.py

import sys
import socket

def tcp_server(port='8080'):
    address = ('0.0.0.0', int(port))
    udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    udps.bind(address)
    tcps = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    tcps.bind(address)
    tcps.listen(1)

    client, addr = tcps.accept()
    while True:
        udpData, udpAddr = udps.recvfrom(255)
        try:
            if udpData == 'go':
                client.send(chr(0x01))
            elif udpData == 'back':
                client.send(chr(0x02))
            elif udpData == 'left':
                client.send(chr(0x03))
            elif udpData == 'right':
                client.send(chr(0x04))
        except:
            client.close()
            client, addr = tcps.accept()

if __name__ == '__main__':
    tcp_server(sys.argv[1])
